﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.SharedComponent.Params.ManagerParam;
using Assessment.Management.BIZ;
using Assessment.Management.SharedComponent.Entities;
using Assessment.Management.Utils;

namespace Assessment.Management.UI.Admin.UserControls.Statistic
{
    public partial class PollAdd : System.Web.UI.UserControl
    {
        #region events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            BindDataToComboboxKhoa();
            lblPollID.Text = GetNextID();
        }

        protected void ddlKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlKhoa.SelectedValue.ToString() == "")
            {
            }
            else
            {
                BindDataToComboboxGVLopMon();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateDataToAdd())
            {
                if (CheckExitsDataConfig())
                {
                    lblMessage.Text = "<font color='Red'>Đã tồn tại dữ liệu này. Vui lòng kiểm tra lại</font>";
                }
                else
                {
                    SavePoll();
                    Response.Redirect("/UI/Admin/UniAdmin.aspx?do=polldisplay&id="+lblPollID.Text);
                }
            }
            else
            {
                lblMessage.Text = "<font color='Red'>Thông tin chưa đầy đủ. Vui lòng kiểm tra lại</font>";
            }
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            if (ValidateDataToAdd())
            {
                if (CheckExitsDataConfig())
                {
                    lblMessage.Text = "<font color='Red'>Đã tồn tại dữ liệu này. Vui lòng kiểm tra lại</font>";
                }
                else
                {
                    SavePoll();
                    Response.Redirect("/UI/Admin/UniAdmin.aspx?do=polladd");
                }
            }
            else
            {
                lblMessage.Text = "<font color='Red'>Thông tin chưa đầy đủ. Vui lòng kiểm tra lại</font>";
            }
        }
        #endregion

        #region methods
        private string GetNextID()
        {
            ConfigUtils config = new ConfigUtils();
            PollParam param = new PollParam(FunctionType.GetLastPollCode);
            MainController.Provider.Execute(param);
            string lastID = config.NextIDHaveDate("BC", param.Poll.ID);
            return lastID;
        }

        private bool CheckExitsDataConfig()
        {
            PollParam param = new PollParam(FunctionType.CheckExitsDataConfig);
            Poll poll = new Poll();
            poll.TeacherID = Convert.ToInt32(ddlTenGV.SelectedValue);
            poll.ClassID = Convert.ToInt32(ddlLop.SelectedValue);
            poll.SubjectID = Convert.ToInt32(ddlMonhoc.SelectedValue);
            param.Poll = poll;
            MainController.Provider.Execute(param);
            bool kt = false;
            if (param.Poll.Count > 0)
            {
                kt = true;
            }
            return kt;
        }

        private void SavePoll()
        {
            PollParam param = new PollParam(FunctionType.CreatePoll);
            Poll poll = new Poll();
            poll.ID = lblPollID.Text.Trim();
            poll.Password = txtPassword.Text.Trim();
            poll.TeacherID = Convert.ToInt32(ddlTenGV.SelectedValue);
            poll.ClassID = Convert.ToInt32(ddlLop.SelectedValue);
            poll.SubjectID = Convert.ToInt32(ddlMonhoc.SelectedValue);
            poll.CreatedBy = SMX.smx_CreatedBy;
            poll.CreatedDTG = DateTime.Now;
            poll.Deleted = SMX.smx_IsNotDeleted;
            poll.Version = SMX.smx_FirstVersion;
            param.Poll = poll;
            MainController.Provider.Execute(param);

            TeacherClassParam paramtc = new TeacherClassParam(FunctionType.CreateTeacherClass);
            TeacherClass tc = new TeacherClass();
            tc.TeacherID = Convert.ToInt32(ddlTenGV.SelectedValue);
            tc.ClassID = Convert.ToInt32(ddlLop.SelectedValue);
            tc.SubjectID = Convert.ToInt32(ddlMonhoc.SelectedValue);
            tc.CreatedBy = SMX.smx_CreatedBy;
            tc.CreatedDTG = DateTime.Now;
            tc.Deleted = SMX.smx_IsNotDeleted;
            tc.Version = SMX.smx_FirstVersion;
            paramtc.TeacherClass = tc;
            MainController.Provider.Execute(paramtc);
        }

        private void BindDataToComboboxKhoa()
        {

            DepartmentParam param = new DepartmentParam(FunctionType.GetListDepartment);
            MainController.Provider.Execute(param);
            ddlKhoa.DataSource = param.listDepartment;
            ddlKhoa.DataBind();
            ddlKhoa.Items.Insert(0, new ListItem("----------Chọn Khoa----------", ""));
            ddlKhoa.SelectedIndex = 0;
        }

        private void BindDataToComboboxGVLopMon()
        {
            TeacherParam paramteacher = new TeacherParam(FunctionType.GetListTeacherByDepartmentID);
            paramteacher.Teacher = new Teacher() { DepartmentID = Convert.ToInt32(ddlKhoa.SelectedValue) };
            MainController.Provider.Execute(paramteacher);
            ddlTenGV.DataSource = paramteacher.listTeacher;
            ddlTenGV.DataBind();
            ddlTenGV.Items.Insert(0, new ListItem("----------Chọn GV----------", ""));
            ddlTenGV.SelectedIndex = 0;

            ClassParam paramclass = new ClassParam(FunctionType.GetListClassByDepartmentID);
            paramclass.Class = new Class() { DepartmentID = Convert.ToInt32(ddlKhoa.SelectedValue) };
            MainController.Provider.Execute(paramclass);
            ddlLop.DataSource = paramclass.listClass;
            ddlLop.DataBind();
            ddlLop.Items.Insert(0, new ListItem("----------Chọn Lớp----------", ""));
            ddlLop.SelectedIndex = 0;

            SubjectParam paramsubject = new SubjectParam(FunctionType.GetListSubjectByDepartmentID);
            paramsubject.Subject = new Subject() { DepartmentID = Convert.ToInt32(ddlKhoa.SelectedValue) };
            MainController.Provider.Execute(paramsubject);
            ddlMonhoc.DataSource = paramsubject.listSubject;
            ddlMonhoc.DataBind();
            ddlMonhoc.Items.Insert(0, new ListItem("----------Chọn Môn----------", ""));
            ddlMonhoc.SelectedIndex = 0;
        }

        private bool ValidateDataToAdd()
        {
            bool kt = true;
            if (ddlKhoa.SelectedValue == "" || ddlTenGV.SelectedValue == "" || ddlLop.SelectedValue == "" || ddlMonhoc.SelectedValue == "")
            {
                kt = false;
            }
            return kt;
        }
        #endregion
    }
}